package com.echoplots.eureka.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.echoplots.eureka.pojo.User;

@RestController
public class TestController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/info")
    public List<ServiceInstance> info() {
    	List<ServiceInstance> instances = client.getInstances("Server-Provider");
    	for (ServiceInstance serviceInstance: instances) {
			
    		String info = "host：" + serviceInstance.getHost() + "，service_id：" + serviceInstance.getServiceId();
    		log.info(info);
		}
        return instances;
    }
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }
    
    @GetMapping("user/{id:\\d+}")
    public User get(@PathVariable Long id) {
        log.info("获取用户id为 " + id + "的信息");
        return new User("echoplots", "123456");
    }
    
    
    @GetMapping("users")
    public List<User> get(String ids) {
        log.info("批量获取用户信息");
        List<User> list = new ArrayList<>();
        for (String id : ids.split(",")) {
            list.add(new User("user" + id, "123456"));
        }
        return list;
    }
}
